package com.my.stock.core;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	public static Date geLastWeekMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getThisWeekMonday(date));
		cal.add(Calendar.DATE, -7);
		return cal.getTime();
	}

	public static Date geLast90(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getThisWeekMonday(date));
		cal.add(Calendar.DATE, -765);
		return cal.getTime();
	}

	public static Date getThisWeekMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// 获得当前日期是一个星期的第几天
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		// 获得当前日期是一个星期的第几天
		int day = cal.get(Calendar.DAY_OF_WEEK);
		// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
		return cal.getTime();
	}

	public static Date getNextWeekMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getThisWeekMonday(date));
		cal.add(Calendar.DATE, 7);
		return cal.getTime();
	}

	public static void main(String[] args) {
		getCloseTime();
		System.out.println(getNowDateTimeF());
		System.out.println(getNowDateTime2());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = new Date();
			System.out.println("今天是" + sdf.format(date));
			System.out.println("上周一" + sdf.format(geLastWeekMonday(date)));
			System.out.println("本周一" + sdf.format(getThisWeekMonday(date)));
			System.out.println("下周一" + sdf.format(getNextWeekMonday(date)));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Date getCloseTime() {
		Date d = new Date();
		d.setHours(15);
		d.setMinutes(18);
		return d;
	}

	public static String getNowDate(long day) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date(System.currentTimeMillis() + day * 3600 * 1000 * 24));
	}
	
	
	public static Date parseDate(String df) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.parse(df);
	}

	public static int getNowHour() {
		Date d = new Date();
		return d.getHours();
	}

	public static int getNowMin() {
		Date d = new Date();
		return d.getHours();
	}

	public static String getDates(long day) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date d = new Date(System.currentTimeMillis() + day * 3600 * 1000 * 24);
		if (d.getDay() == 0 || d.getDay() == 6) {
			return "";
		}

		return sdf.format(d);
	}

	public static String getDatesTrading() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = null;
		if ((new Date()).getHours() > 15) {
			d = new Date(System.currentTimeMillis());
		} else {
			d = new Date(System.currentTimeMillis() + 3600 * 1000 * 24);
		}

		return sdf.format(d);
	}

	public static String getDate(Date d) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(d);
	}

	public static String getNowDate() {
		return getNowDate(0);
	}

	public static String getNowDateC() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date(System.currentTimeMillis() + 3600 * 1000 * 24 *2));
	}

	public static String getNowDateMM() {
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
		return sdf.format(new Date());
	}

	public static String getNowDate1() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		return sdf.format(new Date());
	}

	public static String getNowDate2(long day) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		return sdf.format(new Date(System.currentTimeMillis() + day * 3600 * 1000 * 24));
	}

	public static String getNowDateTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return sdf.format(new Date());
	}

	public static String getNowDateTime1() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		return sdf.format(new Date());

	}

	public static String getNowDateTimeF() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd%20HH:mm:ss");
		return sdf.format(new Date(System.currentTimeMillis() - 3600 * 1000 * 12));

	}

	public static long getNowDateTimeT(String d) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd#HH:mm:ss#SSS");
		try {
			return System.currentTimeMillis() - sdf.parse(d).getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;

	}

	public static String getNowDateTime2() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");

		return sdf.format(new Date(System.currentTimeMillis() + 60000));

	}

	public static double getETime(Date d) {

		int hours = d.getHours();
		int minutes = d.getMinutes();
		int seconds = d.getSeconds();

		double now = (1 / 10e1) * hours + (1 / 10e3) * minutes + (1 / 10e5) * seconds; // 时间格式0.145100 }
		return now;
	}
}